package cn.fbs.service;

import cn.fbs.dao.dm.*;
import cn.fbs.model.NewTCodeTargetType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cache.annotation.Cacheable;
//import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 安逸
 * @date 2021/1/27 - 12:05
 **/
@Service
@Slf4j
public class DataStatisticService {

    @Autowired
    private DMCodeDataStatisticMapper tCodeDataStatisticMapper;

    @Autowired
    private DMLabelGroupInfoMapper labelGroupInfoMapper;

    @Autowired
    private DMDataInfoMapper dataInfoMapper;

    @Autowired
    private DMVideoInfoMapper videoInfoMapper;

    @Autowired
    private DMImageInfoMapper imageInfoMapper;

    @Autowired
    private SysUserMapper userInfoMapper;

    @Autowired
    DMNewTargetMapper newTargetMapper;
    /**
     * 工作量统计
     */
//    public List<Map<String,Object>> queryWorkStatistics(){
//        return tCodeDataStatisticMapper.queryWorkStatistics();
//    }

    /**
     *类别统计
     */
//    public List<Map<String,Object>> queryTypeStatistics(){
//        List<Map<String, Object>> maps = tCodeDataStatisticMapper.queryTypeStatistics();
//        for (Map map : maps){
//
//            if (map.get("type").toString().equals("1")){
//                map.put("type","资料");
//            }else if (map.get("type").toString().equals("2")){
//                map.put("type","图片");
//            }else if (map.get("type").toString().equals("3")){
//                map.put("type","视频");
//            }
//        }
//        return maps;
//    }

    /**
     * 加入九类标签的统计，统计每一类打了多少个
     */
//    public List<Map<String, Object>> queryLabelByTypeStatistics() {
//
//        List<Map<String, Object>> list = labelGroupInfoMapper.queryLabelByTypeStatistics();
//        return list;
//    }

    /**
     * 按用户统计，每个用户新增了多少图像，视频，文件
     */
//    public List<Map<String,List<Map<String,Object>>>> queryDataVideoImageStatistics() {
//
//        List<Map<String, Object>> dataList =  dataInfoMapper.getDataStatistics();
//
//        List<Map<String, Object>> videoList =  videoInfoMapper.getVideoStatistics();
//
//        List<Map<String, Object>> imageList =  imageInfoMapper.getImageStatistics();
//
//        return null;
//    }

    /**
    根据标签组进行统计
     */
    public List<Map<String,Object>> LabelStatistics() {
        List<Map<String, Object>> maps = tCodeDataStatisticMapper.LabelStatistics();
        return maps;
    }
    public void LabelSubStatistics(String labelId){




    }
//    @Cacheable("userStatistics")
    public List<HashMap> userStatistics() {
//        return tCodeDataStatisticMapper.userStatistics();
        List<Map<String, Object>> maps = tCodeDataStatisticMapper.userStatistics();
        List<HashMap> result = dataFormat(maps,"USERNAME");
        return result;

    }


//    public Object userStatistics() {
////        return tCodeDataStatisticMapper.userStatistics();
//
//        /**
//         * 获得提交总量ｔｏｐ3的人,不统计未署名的数据
//         */
//
//        List<String> users=tCodeDataStatisticMapper.getUserTop3();
//
//        ArrayList<String> xAxis = new ArrayList<>();
//        for (String user : users) {
//            xAxis.add(user);
//        }
//        ArrayList<ArrayList> value = new ArrayList<>();
//        ArrayList imageValue = new ArrayList();
//        ArrayList videoValue = new ArrayList();
//        ArrayList dataValue = new ArrayList();
//        value.add(imageValue);
//        value.add(videoValue);
//        value.add(dataValue);
//        ArrayList<String> legend = new ArrayList();
//        legend.add("图像");
//        legend.add("视频");
//        legend.add("文档");
//        for (String type : legend) {
////            String nameStr = StringUtils.join(xAxis, ",");
//            for (String name : xAxis) {
//                Integer num = tCodeDataStatisticMapper.Statistics(type,name);
//                if (type.equalsIgnoreCase("图像")){
//                    if (num==null){
//                        imageValue.add(0);
//
//                    }else {imageValue.add(num);
//                    }
//
//                };
//
//                if (type.equalsIgnoreCase("视频")){
//                    if (num==null){
//                        videoValue.add(0);
//
//                    }else { videoValue.add(num);
//                    }
//
//                };
//
//                if (type.equalsIgnoreCase("视频")){
//                    if (num==null){
//                        dataValue.add(0);
//
//                    }else {dataValue.add(num);
//                    }
//
//                };
//            }
//
//        }
//
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("xAxis",xAxis);
//        map.put("value",value);
//        map.put("legend",legend);
//        return map;
//
////        for (Map<String, Object> map : maps) {
////            Object username = map.get("USERNAME");
////            if (StringUtils.isBlank(username.toString())){
////                map.replace("USERNAME","未知");
////            }
////        }
////        List<HashMap> result = dataFormat(maps,"USERNAME");
////        return result;
//
//    }


//    @Cacheable("timeStatistics")
    public Map<String,Object> timeStatistics() {

        /**
         *对数据前一天，近一周，近一月进行统计
         */
//        List<Map<String,Object>> dayMap=tCodeDataStatisticMapper.timeStatistics("day");
//        List<Map<String,Object>> weekMap=tCodeDataStatisticMapper.timeStatistics("week");
        HashMap<String, Object> resultMap = new HashMap<>();
//        List<HashMap> weekMapList = dataFormat(weekMap,"MYTIME");
//        resultMap.put("weekData",weekMapList);
        /*
        日
         */
        List<Map<String,Object>> dayMap=tCodeDataStatisticMapper.timeStatistics("day");
        List<HashMap> dayDataList = dataFormat(dayMap,"MYTIME");
        resultMap.put("dayDatas",dayDataList);
        /*
        月
         */
        List<Map<String,Object>> monthMap=tCodeDataStatisticMapper.timeStatistics("month");
        List<HashMap> monthDataList = dataFormat(monthMap,"MYTIME");
        resultMap.put("monthDatas",monthDataList);
        /*
        年
         */
        List<Map<String,Object>> yearMap=tCodeDataStatisticMapper.timeStatistics("year");
        List<HashMap>yearDataList = dataFormat(yearMap,"MYTIME");
        resultMap.put("yearDatas",yearDataList);
        return resultMap;
    }

    /**
     * 按照目标进行统计
     * @param zbnm
     * @return
     */
//    @Cacheable(value = "targetStatistics")
    public Map<String,Object> targetStatistics(String zbnm,String source) {
        if ("全部".equalsIgnoreCase(source)){
            source=null;
        }
        HashMap<String, Object> resultMap = new HashMap<>();
        List<String> zbnmList=new ArrayList<>();

        String xh = newTargetMapper.getXh(zbnm);
        int length = xh.length() + 2;
        int count = newTargetMapper.selectCountByXH(length, xh);
        if (count==0){
            //当前目标下没有子目标 将当前目标加入集合
            zbnmList.add(zbnm);
        }else {
            //找到当前目标下的所有子目标
            List<NewTCodeTargetType> list = newTargetMapper.getCurrentChildrens(xh);
            zbnmList = list.stream().map(item -> item.getZbnm()).collect(Collectors.toList());
        }



        String targetTypeStr = StringUtils.join(zbnmList, "','");
        targetTypeStr="'"+targetTypeStr+"'";

        /*
        日
         */
        List<Map<String,Object>> dayMap=tCodeDataStatisticMapper.targetStatistics("day",targetTypeStr,source);
        List<HashMap> dayDataList = dataFormat2(dayMap,"MYTIME");
        resultMap.put("dayDatas",dayDataList);
        /*
        月
         */
        List<Map<String,Object>> monthMap=tCodeDataStatisticMapper.targetStatistics("month",targetTypeStr,source);
        List<HashMap> monthDataList = dataFormat2(monthMap,"MYTIME");
        resultMap.put("monthDatas",monthDataList);
        /*
        年
         */
        List<Map<String,Object>> yearMap=tCodeDataStatisticMapper.targetStatistics("year",targetTypeStr,source);
        List<HashMap>yearDataList = dataFormat2(yearMap,"MYTIME");
        resultMap.put("yearDatas",yearDataList);
        return resultMap;
    }

    private List<HashMap> dataFormat2(List<Map<String,Object>> weekMap,String sourceField) {
        ArrayList<String> imageTime = new ArrayList<>();
        ArrayList<Integer> imageNum = new ArrayList<>();
        ArrayList<String> videoTime = new ArrayList<>();
        ArrayList<Integer> videoNum = new ArrayList<>();
        for (Map<String, Object> map : weekMap) {

            if (map.get("DATATYPE").toString().equalsIgnoreCase("视频")){
                videoTime.add(map.get(sourceField).toString());
                videoNum.add(Integer.parseInt(map.get("NUM").toString()));
            }
            if (map.get("DATATYPE").toString().equalsIgnoreCase("图像")){
                imageTime.add(map.get(sourceField).toString());
                imageNum.add(Integer.parseInt(map.get("NUM").toString()));
            }
        }
        if (sourceField.equalsIgnoreCase("userName")){
            sourceField="user";
        }
        if (sourceField.equalsIgnoreCase("MYTIME")){
            sourceField="time";
        }

        HashMap<String, Object> videoResultMap = new HashMap<>();
        videoResultMap.put("type","视频");
        videoResultMap.put(sourceField.toLowerCase(),videoTime);
        videoResultMap.put("num",videoNum);
        HashMap<String, Object> imageResultMap = new HashMap<>();
        imageResultMap.put("type","图像");
        imageResultMap.put(sourceField.toLowerCase(),imageTime);
        imageResultMap.put("num",imageNum);
        ArrayList<HashMap> weekMapList = new ArrayList<>();
        weekMapList.add(videoResultMap);
        weekMapList.add(imageResultMap);
        return weekMapList;
    }

    private List<HashMap> dataFormat(List<Map<String,Object>> weekMap,String sourceField) {
        ArrayList<String> docTime = new ArrayList<>();
        ArrayList<Integer> docNum = new ArrayList<>();
        ArrayList<String> imageTime = new ArrayList<>();
        ArrayList<Integer> imageNum = new ArrayList<>();
        ArrayList<String> videoTime = new ArrayList<>();
        ArrayList<Integer> videoNum = new ArrayList<>();
        for (Map<String, Object> map : weekMap) {
            if (!map.containsKey("DATATYPE")){
                continue;
            }
            if (map.get("DATATYPE").toString().equalsIgnoreCase("文档")){
                docTime.add(map.get(sourceField).toString());
                docNum.add(Integer.parseInt(map.get("NUM").toString()));
            }
            if (map.get("DATATYPE").toString().equalsIgnoreCase("视频")){
                videoTime.add(map.get(sourceField).toString());
                videoNum.add(Integer.parseInt(map.get("NUM").toString()));
            }
            if (map.get("DATATYPE").toString().equalsIgnoreCase("图像")){
                imageTime.add(map.get(sourceField).toString());
                imageNum.add(Integer.parseInt(map.get("NUM").toString()));
            }
        }
        if (sourceField.equalsIgnoreCase("userName")){
            sourceField="user";
        }
        if (sourceField.equalsIgnoreCase("MYTIME")){
            sourceField="time";
        }
        HashMap<String, Object> docResultMap = new HashMap<>();
        docResultMap.put("type","文档");
        docResultMap.put(sourceField.toLowerCase(),docTime);
        docResultMap.put("num",docNum);
        HashMap<String, Object> videoResultMap = new HashMap<>();
        videoResultMap.put("type","视频");
        videoResultMap.put(sourceField.toLowerCase(),videoTime);
        videoResultMap.put("num",videoNum);
        HashMap<String, Object> imageResultMap = new HashMap<>();
        imageResultMap.put("type","图像");
        imageResultMap.put(sourceField.toLowerCase(),imageTime);
        imageResultMap.put("num",imageNum);
        ArrayList<HashMap> weekMapList = new ArrayList<>();
        weekMapList.add(docResultMap);
        weekMapList.add(videoResultMap);
        weekMapList.add(imageResultMap);
        return weekMapList;
    }


    public List<Map<String, Object>> sumStatistics() {
        Map<String,Object> maps=tCodeDataStatisticMapper.sumStatistics();
        ArrayList<Map<String, Object>> list = new ArrayList();
        Set<String> keySet = maps.keySet();
        for (String key : keySet) {
            Object value = maps.get(key);
            HashMap<String, Object> resultMap = new HashMap<>();
            resultMap.put("typeName",key);
            resultMap.put("num",value);
            list.add(resultMap);
        }
        return list;
    }


    public List<Map<String, Object>> getGroupLableStatistics(String groupId) {
        List<Map<String, Object>> list = tCodeDataStatisticMapper.getGroupLableStatistics(groupId);
        return list;
    }

    public List<Map<String,Object>> getLabelGroupByType(Integer type) {
        List<Map<String,Object>> list =  labelGroupInfoMapper.getLabelGroupByType(type);
        return list;
    }


}
